package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglGlgzMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglGlgz;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglGlgzService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 管理工作
 */
@Service
public class TTgyglGlgzServiceImpl implements TTgyglGlgzService {

    @Autowired
    private TTgyglGlgzMapper tTgyglGlgzMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TTgyglGlgz record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglGlgz record) {
        return 0;
    }

    @Override
    public TTgyglGlgz selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglGlgz record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglGlgz record) {
        return 0;
    }

    @Override
    public int insert(List<JXNode> jxNode, int id) throws Exception {
        System.out.println(id);
        TTgyglGlgz tTgyglGlgz = new TTgyglGlgz();    //月份管理工作

        AtomicInteger yfglgzIndex = new AtomicInteger();  //x月份管理工作位置
        AtomicReference<String> yfglgzInfileIndex = new AtomicReference<>("二、");    //x月份管理工作在文档中的位置标识

        jxNode.forEach(item->{
            if (item.toString().indexOf("月份管理工作")!=-1) { // nextFileIndex
                yfglgzIndex.set(jxNode.indexOf(item));
                yfglgzInfileIndex.set(item.toString().substring(0, 2));
            }
        });

        if (yfglgzIndex.get()==0) { //没有x月份管理工作这一项-->退出
            return 0;
        }

        String nextFileIndexStr = "三、";    // 下一项在文档中的位置标识
        AtomicInteger nextFileIndex = new AtomicInteger();  //下一项在文档中的位置

        if (yfglgzInfileIndex.get().equals("三、")) {
            nextFileIndexStr = "四、";
        } else if (yfglgzInfileIndex.get().equals("四、")) {
            nextFileIndexStr = "五、";
        } else if (yfglgzInfileIndex.get().equals("五、")) {
            nextFileIndexStr = "六、";
        }

        String finalNextFileIndex = nextFileIndexStr;

        jxNode.forEach(item->{
            if (item.toString().indexOf(finalNextFileIndex)!=-1) {
                nextFileIndex.set(jxNode.indexOf(item));
            }
        });


        //截取出 月份管理工作 JXNodeList
        List<JXNode> glgzJxnList = jxNode.subList(yfglgzIndex.get(), nextFileIndex.get());


        System.out.println(glgzJxnList);
        tTgyglGlgz.setContent(glgzJxnList.toString().substring(1, glgzJxnList.toString().length()-1));

        tTgyglGlgz.setFileId(id);
        tTgyglGlgzMapper.insert(tTgyglGlgz);

        return 0;
    }

    @Override
    public List<TTgyglGlgz> selectListByFileId(Integer fileId) {
        List<TTgyglGlgz> resulList = null;
        TTgyglGlgz tTgyglGlgz = new TTgyglGlgz();
        tTgyglGlgz.setFileId(fileId);
        try {
            resulList = tTgyglGlgzMapper.selectByBean(tTgyglGlgz);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resulList;
    }
}
